package com.redside.dao.hibernate;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;

import com.redside.dao.ComplaintDao;
import com.redside.model.Complaint;

/**
 * This class interacts with Spring and Hibernate to save and retrieve Complaint
 * objects.
 * 
 * @author pchapman
 */
public class ComplaintDaoHibernate extends GenericDaoHibernate<Complaint, Long> implements ComplaintDao {

    public ComplaintDaoHibernate() {
        super(Complaint.class);
    }
    
    @SuppressWarnings("unchecked")
    public List<Complaint> findAllUnverified() {
        return getHibernateTemplate().find("from Complaint where business.id IS NULL");
    } 
    
    @SuppressWarnings("unchecked")
    public List<Complaint> findByCountyId(String countyId) {
        return getHibernateTemplate().find(
                "from Complaint where initialComplaint.address.countyId=?", countyId);
    }

    @SuppressWarnings("unchecked")
    public List<Complaint> findUnverifiedByCountyId(String countyId) {
        return getHibernateTemplate().find(
                        "from Complaint where initialComplaint.address.countyId=? and business IS NULL",
                        countyId);
    }

    @SuppressWarnings("unchecked")
    public List<Complaint> findAllForBusinessView(Long businessId) {
        return getHibernateTemplate().find("from Complaint where business.id=?",businessId);
    }
    
    /* (non-Javadoc)
     * @see gov.oregon.wems.dao.complaint.ComplaintDao#findAllCitationComplaints()
     */
    @SuppressWarnings("unchecked")
    public List<Complaint> findAllCitationComplaints() {
        Criteria criteria = getSession().createCriteria(Complaint.class);
        criteria.add(Expression.eq("actionable",true));
        criteria.add(Expression.in("complaintStatusId",
                new Long[]{Long.valueOf(7),Long.valueOf(9),Long.valueOf(10),Long.valueOf(11)}));
        List<Complaint> complaints = criteria.list();
        return complaints;
    }  
    
    /* (non-Javadoc)
     * @see gov.oregon.wems.dao.complaint.ComplaintDao#findCitationComplaintsByCountyId()
     * 
     */
    @SuppressWarnings("unchecked")
    public List<Complaint> findCitationComplaintsByCountyId(String id) {
        DetachedCriteria criteria = null;
        criteria = DetachedCriteria.forClass(Complaint.class, "c");
        criteria.add(Expression.eq("actionable",true));
        criteria.add(Expression.in("complaintStatusId",
                new Long[]{Long.valueOf(7),Long.valueOf(9),Long.valueOf(10),Long.valueOf(11)}));
        
        criteria.createAlias("business", "b");
        logger.debug("Here is the county: '"+id+"'");
        if(!StringUtils.equals(id, "0")) {
            logger.debug("WE ARE ADDING COUNTY TO CRITERIA");
            criteria.add(Expression.eq("b.address.countyId", id));
        }
        List<Complaint> complaints = getHibernateTemplate().findByCriteria(criteria);
        return complaints;
    }      

    /* (non-Javadoc)
     * @see gov.oregon.wems.dao.complaint.ComplaintDao#findUnsentIRL(String countyId, Long corresondenceId, 
            String sql1)
     */
/*    public List<UnsentIRLView> findUnsentIRL(String countyId, Long corresondenceId, 
            String sql1) {
        return null;
    }*/

    /* (non-Javadoc)
     * @see gov.oregon.wems.dao.complaint.ComplaintDao#findUnsentIRL(Long corrId, String sql)
     */
/*    public List <UnsentIRLView>findUnsentIRL(Long corresponceTypeId, String sql1){
        return null;
    }*/
    
    @SuppressWarnings("unchecked")
    public List<Complaint> findComplaintsForVerifyProcess(Long id){
        return getHibernateTemplate().find("from Complaint where business.businessId=?",id);
    }
    
    /**
     * Returns a List of SiteVisitView objects based on county id.
     * @param countyId the County id.
     */
    /* public List<SiteVisitView>siteVisitByCountyId(String countyId){
        Criteria criteria = getSession().createCriteria(Complaint.class);
        if(!StringUtils.equalsIgnoreCase(countyId, "0")){
            criteria.add(Expression.eq("business.countyId", countyId));
        }
        criteria.add(Expression.eq("statusId", Long.valueOf(4)));
        List<SiteVisitView> siteVisits = criteria.list();
        return siteVisits;
        return null;
    }*/

    /* (non-Javadoc)
     * @see gov.oregon.wems.dao.complaint.ComplaintDao#findAllRemediations()
     */
    @SuppressWarnings("unchecked")
    public List<Complaint> findAllRemediations() {
        return getHibernateTemplate().find(
                "from Complaint where actionable = true and complaintStatusId = 5");
    }

    /* (non-Javadoc)
     * @see gov.oregon.wems.dao.complaint.ComplaintDao#findRemediationByCountyId(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public List<Complaint> findRemediationByCountyId(String countyId) {
        return getHibernateTemplate().find(
        "from Complaint where actionable = true and complaintStatusId = 5 and business.address.countyId = ?",countyId);
    }

    /*
     * (non-Javadoc)
     * @see gov.oregon.wems.dao.complaint.ComplaintDao#findByBusinessId(java.lang.Long)
     */
    @SuppressWarnings("unchecked")
    public List<Complaint> findByBusinessId(Long businessId) {
        return getHibernateTemplate().find("from Complaint where business.businessId=?",businessId);
    }

}